package test.test_7_15;

import java.io.*;

public class Demo01 {
    public static void main(String[] args) {
        new Thread(new copyFileRunnable(
                new File("D:\\wangdao_study\\studyHard\\风景.jpg")
                , new File("风景111.jpg"))).start();


    }
}

class copyFileRunnable implements Runnable {
    File oldFIle;
    File newFile;

    public copyFileRunnable(File file) {
    }

    public copyFileRunnable(File oldFIle, File newFile) {
        this.oldFIle = oldFIle;
        this.newFile = newFile;
    }

    @Override
    public void run() {
        //复制文件操作
        try {
            FileInputStream fis = new FileInputStream(oldFIle);
            FileOutputStream fos = new FileOutputStream(newFile);

            //边读边写
            int readCount;

            byte[] bytes = new byte[1024];
            //用来做累加
            double temp = 0;
            long length = oldFIle.length();//获取被复制文件的字节数
            while ((readCount = fis.read(bytes)) != -1) {
                //开始复制文件操作
                fos.write(bytes);
                temp = temp + readCount;
                double persent = 0.0;
                persent = temp / length * 100;
                System.out.println(oldFIle.getName() + "复制了：" + String.format("%.2f", persent) + "%");
            }
            System.out.println(oldFIle.getName() + "复制完成！！！");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
